﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Data;

using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;
using DevExpress.Web.ASPxGridView;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;

public partial class CARByCAR : ICPRelationshipPage
{
    public String m_sxCarId = String.Empty;
    #region Properties
    
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Change CAR By CAR";
        if (!IsPostBack && !IsCallback)
        {
            Session[KSession.CARBYCAR_CARID] = null;
        }
        InitData();
    }
    protected void dgCAR_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {
        if (e.Parameters.ToUpper().StartsWith("SELECT"))
        {
            HandleSelect(e.Parameters);
        }
        else if (e.Parameters.ToUpper().StartsWith("CHANGE"))
        {
            HandleRefresh();
        }
    }
    protected void cbkCAR_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        if(e.Parameter.ToUpper().StartsWith("CHANGE"))
        {
            HandleChange(e.Parameter);
        }
    }
    
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected void HandleSelect(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        Session[KSession.CARBYCAR_CARID] = sxSplitted[1];
        m_sxCarId = Session[KSession.CARBYCAR_CARID].ToString();
        Cached.CompanyTable cachedCompany = new Cached.CompanyTable();
        DataTable dt = cachedCompany.GetCompanyList_ByCar(m_sxCarId);

        dgCAR.DataSource = dt;
        dgCAR.DataBind();
    }
    protected void InitData()
    {
        Cached.CompanyTable cachedCompany = new Cached.CompanyTable();
        if (Session[KSession.CARBYCAR_CARID] != null)
        {
            m_sxCarId = (String)Session[KSession.CARBYCAR_CARID];
        }
        DataTable dt = cachedCompany.GetCompanyList_ByCar(m_sxCarId);

        dgCAR.DataSource = dt;
        dgCAR.DataBind();
    }
    protected void HandleChange(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxOldCarId = sxSplitted[1];
        String sxCarId = sxSplitted[2];
        //GET SELECTED CUSTOMER_ID
        List<object> keyValues = dgCAR.GetSelectedFieldValues("CUSTOMER_ID");
        // BEGIN TRANSACTION
        DBLib dbl = new DBLib();
        dbl.BeginTransaction();
        try
        {
            foreach (object obj in keyValues)
            {
                int iCompanyId = Convert.ToInt32(obj);
                DBQuery.CARSpocTable.ChangeCARByCompany(dbl,iCompanyId,sxCarId);
            }
            // COMMIT 
            dbl.EndTransaction(true);
            //Refresh Cache
            Cached.CompanyTable cachedCompany = new Cached.CompanyTable();
            cachedCompany.GetCompanyList_ByCar(sxOldCarId, true);
            cachedCompany.GetCompanyList_ByCar(sxCarId, true);
        }
        catch (Exception ex)
        {
            // ROLLBACK
            dbl.EndTransaction(false);
            throw ex;
        }
        //DBQuery.CARSpocTable.ChangeCARByCAR(sxOldCarId, sxCarId);
        
    }
    protected void HandleRefresh()
    {

        dgCAR.DataSource = null;
        dgCAR.DataBind();
        dgCAR.Selection.UnselectAll();
    }
    #endregion Custom Methods
}
